﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LKCMS.Common;

namespace LKCMS.Web.Admin.Pictures
{
    public partial class Add : LKCMS.Web.UI.ManagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                chkLoginLevel("addPictures");
                //绑定类别
                ChannelTreeBind(0, "请选择所属类别...", (int)Channel.Pictures, this.ddlClassId);
                FieldBind();
                if (!string.IsNullOrEmpty(Request.Params["classId"]))
                {
                    ddlClassId.SelectedValue = Request.Params["classId"].Trim();
                }
            }
        }

        //绑定扩展字段
        private void FieldBind()
        {
            LKCMS.BLL.PicturesField bll = new LKCMS.BLL.PicturesField();
            this.rptField.DataSource = bll.GetList("");
            this.rptField.DataBind();
        }

        //输出输出扩展字段的CSS
        protected string WriteCss(bool isNull, string fieldType)
        {
            StringBuilder str = new StringBuilder();
            str.Append("input");
            if (isNull == false)
                str.Append(" required");
            if (fieldType.Trim() == "Numeric")
                str.Append(" number");
            return str.ToString();
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            LKCMS.Model.Pictures model = new LKCMS.Model.Pictures();
            LKCMS.BLL.Pictures bll = new LKCMS.BLL.Pictures();

            model.Title = txtTitle.Text.Trim();
            //取得图片集合,添加相片
            string firstImg = "";
            string hideFiles = Request.Params["hideFiles"];
            if (!string.IsNullOrEmpty(hideFiles))
            {
                string[] fileArr = hideFiles.Split(',');
                //生成缩略图
                firstImg = MakeThumbnail(fileArr[0]);
            }
            model.ImgUrl = firstImg;
            model.Price = decimal.Parse(txtPrice.Text.Trim());
            model.ClassId = int.Parse(ddlClassId.SelectedValue);
            model.Content = txtContent.Value;
            model.Click = int.Parse(txtClick.Text.Trim());
            model.SortId = int.Parse(txtSortId.Text.Trim());

            model.IsTop = 0;
            model.IsRed = 0;
            model.IsHot = 0;

 
            if (cblItem.Items[0].Selected == true)
            {
                model.IsTop = 1;
            }
            if (cblItem.Items[1].Selected == true)
            {
                model.IsRed = 1;
            }
            if (cblItem.Items[2].Selected == true)
            {
                model.IsHot = 1;
            }
        

            //保存相册
            if (!string.IsNullOrEmpty(hideFiles))
            {
                List<LKCMS.Model.PicturesAlbum> als = new List<LKCMS.Model.PicturesAlbum>();
                string[] fileArr = hideFiles.Split(',');

                foreach (string str in fileArr)
                {
                    als.Add(new LKCMS.Model.PicturesAlbum { ImgUrl = str });
                }
                model.PicturesAlbums = als;
            }

            //扩展字段
            List<LKCMS.Model.PicturesExtension> els = new List<LKCMS.Model.PicturesExtension>();
            for (int i = 0; i < rptField.Items.Count; i++)
            {
                int _fieldid = Convert.ToInt32(((HiddenField)rptField.Items[i].FindControl("hideFieldId")).Value);
                string _title = ((HiddenField)rptField.Items[i].FindControl("hideFieldTitle")).Value;
                string _content = ((TextBox)rptField.Items[i].FindControl("txtFieldContent")).Text;
                els.Add(new LKCMS.Model.PicturesExtension { FieldId = _fieldid, FieldName = _title, Content = _content });
            }
            model.PicturesExtensions = els;

            int ReId = bll.Add(model);
            if (ReId > 0)
            {
                //保存日志
                SaveLogs("[图文模块]添加图文：" + model.Title);
                JscriptPrint("图文发布成功啦！", "Add.aspx?classId=" + ddlClassId.SelectedValue, "Success");
            }
            else
            {
                JscriptPrint("发布过程中发错误啦！", "", "Error");
            }
        }

    }
}
